<?php declare(strict_types=1);

namespace App\Services\Pay\GuoTonXinYi;

use App\Exceptions\BizException;
use App\Services\Pay\ResultVo;
use Illuminate\Support\Facades\Log;

class SignGt
{
    public static function openPersion(array $params, array $config): ResultVo
    {
        try {
            //agetId public_key
            $url = Config::getDomain() . Config::anxinsignPerson;
            $reqData = [
                "agetId" => $config["agetId"] ?? "",
                "applyAgetId" => $config["agetId"],
                "custLogin" => $params["custLogin"] ?? "",
                "custName" => $params["custName"] ?? "",
                "credNo" => $params["credNo"] ?? "",
                "phone" => $params["phone"] ?? "",
                "accountName" => $params["accountName"] ?? "",
                "accountNo" => $params["accountNo"] ?? "",
                "version" => Config::VERSION,
                "timeStamp" => date("YmdHis", time())
            ];
            $sign = Config::getSign($reqData, $config["public_key"]);
            $reqData["sign"] = $sign;
            $resultArray = Config::post($url, $reqData);
            Log::channel("elec")->info("openPersion", [
                "url" => $url,
                "reqData" => $reqData,
                "resData" => $resultArray
            ]);
            if (!isset($resultArray["code"]) || $resultArray["code"] != "000000") {
                throw new BizException($resultArray["msg"]);
            }
            if (!isset($resultArray["data"])) {
                throw new BizException($resultArray["msg"]);
            }
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("failed")::data(null)::msg($e->getMessage());
        }
        return $resultVo;
    }

    public static function openCompany(array $params, array $config): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::anxinsignCompany;
            $reqData = [
                "agetId" => $config["agetId"],
                "applyAgetId" => $config["agetId"],
                "custLogin" => $params["custLogin"],
                "enterpriseName" => $params["enterpriseName"],
                "identType" => $params["identType"],
                "identNo" => $params["identNo"],
                "phone" => $params["phone"],
                "licenseNo" => $params["licenseNo"],
                "enterprisePhone" => $params["enterprisePhone"],
                "address" => $params["address"],
                "transName" => $params["transName"],
                "transNo" => $params["transNo"],
                "accountName" => $params["accountName"],
                "accountNo" => $params["accountNo"],
                "credNo" => $params["credNo"],
                "busName" => $params["busName"],
                "version" => Config::VERSION,
                "timeStamp" => date("YmdHis", time()),
            ];
            $sign = Config::getSign($reqData, $config["public_key"]);
            $reqData["sign"] = $sign;
            $resultArray = Config::post($url, $reqData);
            Log::channel("elec")->info("openCompany", [
                "url" => $url,
                "reqData" => $reqData,
                "resData" => $resultArray
            ]);
            if (!isset($resultArray["code"]) || $resultArray["code"] != "000000") {
                throw new BizException($resultArray["msg"]);
            }
            if (!isset($resultArray["data"])) {
                throw new BizException($resultArray["msg"]);
            }
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("failed")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }


    public static function sms(array $params, array $config): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::anxinsignSendCode;
            $reqData = [
                "agetId" => $config["agetId"],
                "custLogin" => $params["custLogin"],
                "identNo" => $params["identNo"],
                "phone" => $params["phone"],
                "isSendVoice" => "0",
                "userId" => $params["userId"],
                "version" => Config::VERSION,
                "timeStamp" => date("YmdHis", time())
            ];
            $sign = Config::getSign($reqData, $config["public_key"]);
            $reqData["sign"] = $sign;
            $resultArray = Config::post($url, $reqData);
            Log::channel("elec")->info("发送短信验证码", [
                "url" => $url,
                "req" => $reqData,
                "res" => $resultArray
            ]);
            if (!isset($resultArray["code"]) || $resultArray["code"] != "000000") {
                throw new BizException($resultArray["msg"]);
            }
            if (!isset($resultArray["data"])) {
                throw new BizException($resultArray["msg"]);
            }
            $resultVo = ResultVo::code("ok")::data($resultArray["data"])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("failed")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

    public static function checkCode(array $params, array $config): ResultVo
    {
        try {
            $url = Config::getDomain() . Config::anxinsignCheckCode;
            $reqData = [
                "agetId" => $config["agetId"],
                "checkCode" => $params["checkCode"],
                "projectCode" => $params["projectCode"],
                "version" => Config::VERSION,
                "timeStamp" => date("YmdHis", time())
            ];
            $sign = Config::getSign($reqData, $config["public_key"]);
            $reqData["sign"] = $sign;
            $resultArray = Config::post($url, $reqData);
//            {"msg":"验证码校验通过!","code":"000000","success":true}}
            Log::channel("elec")->info("checkCode", [
                "url" => $url,
                "reqData" => $reqData,
                "resData" => $resultArray
            ]);
            if ($resultArray["code"] != "000000") {
                throw new BizException("验证失败");
            }
            $resultVo = ResultVo::code("ok")::data([])::msg($resultArray["msg"]);
        } catch (\Exception $e) {
            $resultVo = ResultVo::code("failed")::data(null)::msg($e->getMessage() . "File:" . $e->getFile() . "Line:" . $e->getLine());
        }
        return $resultVo;
    }

}